package com.distribute.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.distribute.common.Constant;
import com.distribute.common.exception.HystrixException;
import com.distribute.dto.LoginUser;
import com.distribute.entity.TOrderItem;
import com.distribute.entity.TSysUser;
import com.distribute.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

@CrossOrigin
// @CrossOrigin(origins = "http://172.16.71.27:8080")
@RequestMapping("/member")
@RestController
public class UserController {
	
	private static int num = 1;
	
	@Autowired
	private UserService userService;
	
	@PostMapping
	public TSysUser saveUser(@RequestBody TSysUser user) {
		return userService.save(user);
	}
	
	// 无权限测试
	@PreAuthorize("hasAuthority('test')")
	@GetMapping("/noAuthority")
	public void test(HttpSession session) {
		System.out.println(11);
	}
	
	// 有权限测试
	@PreAuthorize("hasAuthority('sys:user:add')")
	@GetMapping("/hasAuthority")
	public TSysUser getSysUser(HttpSession session) {
		LoginUser loginUser = (LoginUser) session.getAttribute(Constant.SESSION_USER);
		return loginUser;
	}
	
	// 限流测试
	@GetMapping
	@HystrixCommand(
			fallbackMethod = "getByIdFallback",
			threadPoolProperties = {  //10个核心线程池,超过20个的队列外的请求被拒绝; 当一切都是正常的时候，线程池一般仅会有1到2个线程激活来提供服务
					@HystrixProperty(name = "coreSize", value = "10"),
					@HystrixProperty(name = "maxQueueSize", value = "100"),
					@HystrixProperty(name = "queueSizeRejectionThreshold", value = "20")},
			commandProperties = {
					@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000"), //命令执行超时时间
					@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"), //若干10s一个窗口内失败三次, 则达到触发熔断的最少请求量
					@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "30000") //断路30s后尝试执行, 默认为5s
      })
	public TSysUser getById(String name) {
		if(num == 1) {
			num = 0;
			try {
				Thread.sleep(20000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} 
		} else {
			num = 1;
		}
		return userService.getByName(name);
	}
	public TSysUser getByIdFallback(Long id) {
		throw new HystrixException("服务器负载过重，请稍后请求!");
	}
	
	
	// 分表测试 
	@PreAuthorize("hasAuthority('sys:user:add')")
	@GetMapping("/shardingsphere")
	@ResponseBody
	public List<TOrderItem> testJdbc() {
		List<TOrderItem> shardingJdbc = userService.shardingJdbc();
		return shardingJdbc;
	}
	
	// 分布式事务测试  -> member-controller -> member-service -> admin-service -> portal-service
	@GetMapping("/lcn")
	@ResponseBody
	public String testTransaction() {
		try {
			userService.testTransaction();
		} catch (Exception e) {
			return "false";
		}
		return "true";
	}
	
	
}
